<?php

define('ACC',true);

require ('../includes/init.php');
require ('left.php');

if(isset($_SESSION['user_id']) && isset($_SESSION['username'])){
	$smarty->assign('SESSION', $_SESSION);
}

$cart=cart::getCart();
$flow = new FlowModel();

if($_GET['act']=='buy'){

	$goodsModel=new GoodsModel();
	$goods_id=$_GET['goods_id']+0;
	$goods=$goodsModel->detail($goods_id);

	if(empty($goods)){
		msg($smarty,'此商品不存在');
	}

	$cart->addItem($goods_id,$goods['goods_name'],$goods['shop_price'],1);

	header('location: flow.php?act=list');
}

if($_GET['act']=='list'){

	$goods_list=$cart->items();
	$price=$cart->getPrice();
	if(empty($goods_list)){
		$goods = array();
	}
	
	$i = 0;
	foreach($goods_list as $key => $val){
		$goods[$i] = $flow->get_good_info($key);
		$goods[$i]['num'] = $val['num'];
		$goods[$i]['total'] = $val['shop_price'] * $val['num'];
		++$i;
	}

	$smarty->assign("goods",$goods);
	$smarty->assign("price",$price);
	$smarty->assign('action', 'cart');
	$smarty->display('./front/flow.html');
	exit;
}

if($_GET['act']=='checkOut'){

	if($cart->cnt()==0){
		$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>&nbsp;系统提示');
		$smarty->assign('action', 'return');
		$smarty->assign('url', 'javascript:history.back()');
		msg($smarty, '购物车为空');
	}
	
	if(!isset($_SESSION['user_id']) || !isset($_SESSION['username'])){
		$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>&nbsp;系统提示');
		$smarty->assign('action', 'login');
		$smarty->assign('url', 'user.php?act=login');
		msg($smarty, '请先登录！');
	}
	if(!($userAddresss = $flow->getUserAddress($_SESSION['user_id']))){
		$smarty->assign('action', 'consignee');
		$smarty->display('./front/flow.html');
		exit;
	}
	
	$goods_list=$cart->items();
	$i = 0;
	foreach($goods_list as $key => $val){
		$goods[$i] = $flow->get_good_info($key);
		$goods[$i]['num'] = $val['num'];
		$goods[$i]['total'] = $val['shop_price'] * $val['num'];
		++$i;
	}

	$price=$cart->getPrice();
	$consignee = $flow->getConsignee($_SESSION['user_id']);
	$shippings = $flow->getShippings();

	$smarty->assign('shippings', $shippings);
	$smarty->assign('consignee', $consignee);
	$smarty->assign("goods",$goods);
	$smarty->assign("price",$price);
	$smarty->assign('action', 'orderConfirm');
	$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;</code>&nbsp;购物流程');
	$smarty->display('./front/flow.html');
	exit;
}



if(!isset($_SESSION['user_id']) || !isset($_SESSION['username'])){
	$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;&nbsp;</code>&nbsp;系统提示');
	$smarty->assign('action', 'login');
	$smarty->assign('url', 'user.php?act=login');
	msg($smarty, '请先登录！');
}

if($_GET['act']=='done'){

	$list=$cart->items();
	$price=$cart->getPrice();

	if($cart->cnt()==0){
		msg($smarty,'购物车为空');
	}

	$address_id = $_POST['address_id2'] + 0;
	$user_address = $flow->get_user_address($address_id);
	$time=time();
	$data = array();
	foreach($list as $k=>$v){
		
		$data['goods_id'] = $k;
		$data['goods_number'] = $list[$k]['num'];
		$data['order_sn'] = date('ymd-',$time).rand(1000,9999).$k;
		$data['user_id'] = isset($_SESSION['user_id'])?$_SESSION['user_id']:0;
		
		$data['country'] = $user_address['country'];
		$data['province'] = $user_address['province'];
		$data['city'] = $user_address['city'];
		$data['district'] = $user_address['district'];
		$data['consignee'] = $user_address['consignee'];
		$data['email'] = $user_address['email'];
		$data['address'] = $user_address['address'];
		$data['zipcode'] = $user_address['zipcode'];
		$data['tel'] = $user_address['tel'];
		$data['mobile'] = $user_address['mobile'];
		$data['postscript'] = trim($_POST['postscript']);
		$data['pay_status'] = 1;
		
		if(isset($_POST['shipping'])){
			$data['shipping_id'] = $_POST['shipping'] + 0;
		}
		
		$data['amount'] = $cart->getPrice();
		$data['addtime'] = $time;
		$addTime = date("Y-M-D H:i",$time);
	}

	$order_id=$flow->add($data);

	if($order_id===false){
		msg($smarty,'下单失败');
	}

	$item=$cart->items();
	$succ=array();
	foreach($item as $k=>$v){
		$data2=array();
		$data2['order_id']=$order_id;
		$data2['goods_id']=$k;
		$data2['goods_name']=$v['name'];
		$data2['num']=$v['num'];
		$data2['shop_price']=$v['shop_price'];
		$data2['subtotal']=$v['shop_price']*$v['num'];

		if($tmp=$flow->addGoods($data2)){
			$succ[]=$tmp;
		}
	}

	if($cart->cnt()!=count($succ)){
		$flow->invoke($order_id);
		msg($smarty,'下单失败');
	}

	$cart->clear();

	$smarty->assign("order_sn",$data['order_sn']);
	$smarty->assign("addTime",$data['addtime']);
	$smarty->assign('ur_here', '当前位置:&nbsp;<a href=".">首页</a><code>&nbsp;&gt;</code>&nbsp;购物流程');
	$smarty->assign('action', 'shopSuccess');
	$smarty->display('./front/flow.html');
	exit;

}

if($_GET['act'] == 'consignee_update'){

	$address_id = $_GET['address_id'] + 0;

	$address = $flow->get_address($address_id);

	$smarty->assign('address', $address);
	$smarty->assign('action', 'consignee_update');
	$smarty->display('./front/flow.html');
	exit;

}

if($_GET['act'] == 'regionSearch'){
	$region_id = $_GET['region_id'] + 0;
	$regions = $flow->getRegions($region_id);
	echo json_encode($regions);
	return;
}

if($_GET['act'] == 'regionInsert'){
	$data['user_id'] = $_SESSION['user_id'];
	$data['is_apply'] = 1;
	$data['country'] = trim($_POST['country']);
	$data['province'] = trim($_POST['province']);
	$data['city'] = trim($_POST['city']);
	$data['district'] = trim($_POST['district']);
	$data['consignee'] = trim($_POST['consignee']);
	$data['email'] = trim($_POST['email']);
	$data['address'] = trim($_POST['address']);
	$data['zipcode'] = $_POST['zipcode'] + 0;
	$data['tel'] = $_POST['tel'] + 0;
	$data['mobile'] = $_POST['mobile'] + 0;
	$flow -> regionInsert($data, 'user_address', $data['user_id']);
	header("Location: flow.php?act=checkOut");
}

if($_GET['act'] == 'total'){
	
	$goodsModel = new GoodsModel();
	$goods_id = $_GET['goods_id'] + 0;
	$num = trim($_GET['num']);
	if(is_numeric($num)){
		if(($num = floor($num)) < 0){
			$num = 1;
			$arr['num'] = 1;
		}else{
			$arr['num'] = $num;
		}
	}else{
		$num = 1;
		$arr['str'] = 1;
	}
	
	$goods=$goodsModel->detail($goods_id);
	
	$cart->set($goods_id, $num);

	$arr['total'] = $goods['shop_price'] * $num;
	$arr['price'] = $cart->getPrice();
	
	echo json_encode($arr);
	return;
	
}

if($_GET['act'] == 'goods_del'){
	
	$goods_id = $_GET['goods_id'] + 0;
// 	$basket = $cart->Item();
// 	$num = $basket[$goods_id]['num'];
// 	$shop_price = $basket[$goods_id]['shop_price'];
// 	$shop_price = $num * $shop_price;
	
	$cart->delItem($goods_id);
	$price = $cart->getPrice();
	
	echo $price;
	return;
	
}

if($_GET['act'] == 'address_update'){
	
	$address_id = $_GET['address_id'] + 0;
	
	$data['country'] = trim($_POST['country']);
	$data['province'] = trim($_POST['province']);
	$data['city'] = trim($_POST['city']);
	$data['district'] = trim($_POST['district']);
	$data['consignee'] = trim($_POST['consignee']);
	$data['email'] = trim($_POST['email']);
	$data['address'] = trim($_POST['address']);
	$data['zipcode'] = $_POST['zipcode'] + 0;
	$data['tel'] = $_POST['tel'] + 0;
	$data['mobile'] = $_POST['mobile'] + 0;
	$data['is_apply'] = 1;

	$where = ' address_id = ' . $address_id ;
	if($flow -> addressUpdate($data, 'user_address', $where)){
		header("Location: flow.php?act=checkOut");
	}
	
}

if($_GET['act'] == 'count'){
	
	$shipping_id = $_GET['shipping_id'] + 0;
	$shipping_price = $flow->get_shipping_price($shipping_id);

	echo $shipping_price;
	return;
	
}

if($_GET['act'] == 'clear'){
	$cart->clear();
	header("Location: flow.php?act=list");
	exit;
	
}




/* if($_GET['act']=='list2'){

if(isset($_GET['goods_id']) && isset($_GET['number'])){
$goods_id=$_GET['goods_id']+0;
$goods_number=$_GET['number']+0;
$cart=$_SESSION['cart'];
$cart->modItem($goods_id, $goods_number);
$_SESSION['cart']=$cart;
$list=$cart->items();
if($list[$goods_id]==$goods_id && $list[$goods_id]['num']==$goods_number){
return 0;
}
return 1;
}
} */







